package net.wanho.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-08-13 17:05
 * Description：<描述>
 */
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("添加用户")
    @PostMapping
    public AjaxResult add(@ApiParam("用户信息") @RequestBody User user){
        userService.save(user);
        return AjaxResult.success("添加成功");
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@ApiParam("用户id") @PathVariable Integer id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation("分页查询用户信息")
    @GetMapping("/findPage")
    public Page<User> findPage(@ApiParam("页码") @RequestParam(required = false, defaultValue = "1") int pageNum,
                               @ApiParam("页大小")@RequestParam(required = false, defaultValue = "5") int pageSize){
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> page2 = userService.page(page);
        System.out.println(page==page2);

        return page;
    }

    @ApiOperation("修改用户")
    @PutMapping
    public AjaxResult modify(@ApiParam("用户信息") @RequestBody User user){
        userService.updateById(user);
        return AjaxResult.success();
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public AjaxResult findAll(){
        List<User> list = userService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Integer id){
        User user = userService.getById(id);
        return AjaxResult.success(user);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam("用户名") String username,
                            @ApiParam("密码") String password){
        User user = userService.login(username,password);
        return AjaxResult.success(user);
    }

    @ApiIgnore
    @GetMapping("/query")
    public AjaxResult query(String username){
        // QueryWrapper<User> wrapper = new QueryWrapper<>();
        // wrapper.like("username", username); // % username %
        // wrapper.like(!StringUtils.isEmpty(username), "username", username);
        // wrapper.orderByDesc()

        // 使用Lambda语法
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.like(User::getUsername, username)
                .gt(User::getId, 2); // 使用方法引用，可以避免写错字段名


        List<User> list = userService.list(wrapper);
        return AjaxResult.success(list);
    }
}
